package chase.myrewards;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.rdbms.AppEngineDriver;
import com.google.appengine.api.utils.SystemProperty;

import chase.myrewards.beans.Customer;

/**
 * Servlet for logging user out of session.  Updates settings in the database
 * then deletes the session object.
 * 
 * @author Meena Abdou
 * @date 05/12/2013
 */
public class LogoutServlet extends HttpServlet implements Serializable {
	
	/**
	 * This number must be unique for every class. And it can never change.
	 * You have to generate it automatically by clicking on the class name and
	 * then hitting Ctrl+1.  Then click on Add generated serial version ID. Then
	 * copy these comments on top of the generated id.
	 * 
	 * @serial serialVersionUID
	 */
	private static final long serialVersionUID = -2917634205012716195L;

	private static final Logger log = Logger.getLogger(LogoutServlet.class.getName());

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		log.info("Session Id is " + request.getSession().getId());
		
		// The app is running on App Engine
		if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
			log.info("In LogoutServlet");
			
			Connection c = null;
			try {				
				// Get connection to database
				DriverManager.registerDriver(new AppEngineDriver());
				c = DriverManager.getConnection("jdbc:google:rdbms://myrewards2013:myrewardsdatabase/myrewards");
				
				// Get Customer object from request
				ObjectInputStream in = new ObjectInputStream(request.getInputStream());
				Customer cust = (Customer) in.readObject();
				log.info(cust.toString());
				
				// Destroy all session objects by invalidating the session
				request.getSession().invalidate();
				
				// Update fields in database
				java.sql.Statement stmt =  c.createStatement();
				stmt.executeUpdate(Queries.CUSTOMER_UPDATE(cust));
			}
			catch(IOException i){
				log.log(Level.SEVERE, "IOException", i);
			} 
			catch (ClassNotFoundException e) {
				log.log(Level.SEVERE, "ClassNotFound", e);
			}
			catch (SQLException e) {
				log.log(Level.SEVERE, "SQLException", e);
			} 
			finally {
				if (c != null){ 
					try {
						c.close();
					} 
					catch (SQLException ignore) {}
				}
			}
		}

		// Running on development server
		else {
			log.info("In development mode of LogoutServlet");
			try {
				ObjectInputStream in = new ObjectInputStream(request.getInputStream());
				Customer cust = (Customer) in.readObject();
				log.info(cust.toString());
				log.info(Queries.CUSTOMER_UPDATE(cust));
				request.getSession().invalidate();
			}
			catch(IOException i){
				log.log(Level.SEVERE, "IOException", i);
			} 
			catch (ClassNotFoundException e) {
				log.log(Level.SEVERE, "ClassNotFound", e);
			}
		}
		return;
	}
}
